package cm.just;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "MethodServlet",value = "/methodServlet")
/**
 * name参数 为servlet类的名字
 * value参数 为访问路径
 */
public class MethodServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        if(req.getMethod().equals("GET")){
            doGet(req, resp);
        }else{
            doPost(req, resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8"); //解决post提交中文乱码的问题
        System.out.println("doPost方法响应");
        String postName = request.getParameter("postName");
        System.out.println(postName);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("doGet方法响应");
        String getName = request.getParameter("getName");
        System.out.println(getName);

    }
}
